package com.kekezu.kppw.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.kekezu.kppw.utils.ConfigInc;
import com.kekezu.kppw.utils.HttpUtils;

public class WorkDetailData {
	/**
	 * 作品（服务）详情
	 * 
	 * @param type
	 * @param shopId
	 * @param context
	 * @return
	 */
	public static Map<String, String> getWorkDetails(String type,
			String goodId, Context context) {
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "shop/goodContent?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();
		params.put("id", goodId);
		params.put("type", type);

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);

			Log.e("strJson", strJson);

			resMap.put("code", jsonObject.getString("code"));

			// 对返回的数据进行判断
			if (jsonObject.getString("code").equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));
				resMap.put("id", jsondata.getString("id"));
				resMap.put("shop_id", jsondata.getString("shop_id"));
				resMap.put("title", jsondata.getString("title"));
				resMap.put("cash", jsondata.getString("cash"));
				resMap.put("unit", jsondata.getString("unit"));
				resMap.put("cover", jsondata.getString("cover"));
				resMap.put("sales_num", jsondata.getString("sales_num"));
				resMap.put("desc", jsondata.getString("desc"));

				resMap.put("comments_num", jsondata.getString("comments_num"));
				resMap.put("good_comment", jsondata.getString("good_comment"));
				resMap.put("uid", jsondata.getString("uid"));
				resMap.put("percent", jsondata.getString("percent"));
				resMap.put("city_name", jsondata.getString("city_name"));
				resMap.put("username", jsondata.getString("username"));

				resMap.put("avg_score", jsondata.getString("avg_score"));
				resMap.put("speed_score", jsondata.getString("speed_score"));
				resMap.put("quality_score", jsondata.getString("quality_score"));
				resMap.put("attitude_score",
						jsondata.getString("attitude_score"));
			} else {
				resMap.put("message", jsonObject.getString("message"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}

	/**
	 * 作品（服务）内容
	 * 
	 * @param type
	 * @param shopId
	 * @param context
	 * @return
	 */
	public static Map<String, String> getWorkContent(String type,
			String shopId, Context context) {
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "shop/goodDetail?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();
		params.put("id", shopId);
		params.put("type", type);

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));

				resMap.put("id", jsondata.getString("id"));
				resMap.put("shop_id", jsondata.getString("shop_id"));
				resMap.put("uid", jsondata.getString("uid"));
				resMap.put("desc", jsondata.getString("desc"));

			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}

	/**
	 * 作品（服务）评价
	 * 
	 * @param type
	 * @param shopId
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, String>> getWorkEvsluate(
			String type, String shopId, String Sorts, int page, Context context) {
		ArrayList<HashMap<String, String>> taskList = new ArrayList<HashMap<String, String>>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "shop/goodComment?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();
		params.put("id", shopId);
		params.put("type", type);
		params.put("sorts", Sorts);
		params.put("page", "" + page);

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			// String resMsg = jsonObject.getString("message");
			String resData = jsonObject.getString("data");

			JSONObject jsondata = new JSONObject(resData);

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				if (jsondata.getString("commentInfo").equals("[]")) {
					return taskList;
				} else {
					JSONObject jsonInfo = jsondata.getJSONObject("commentInfo");
					JSONArray jsonArray = jsonInfo.getJSONArray("data");
					if (jsonArray.length() > 0) {
						for (int i = 0; i < jsonArray.length(); i++) {
							HashMap<String, String> map = new HashMap<String, String>();
							JSONObject itemJsonObject = jsonArray
									.optJSONObject(i);

							map.put("total", jsonInfo.getString("total"));
							map.put("good_num", jsondata.getString("good_num"));
							map.put("middle_num",
									jsondata.getString("middle_num"));
							map.put("bad_num", jsondata.getString("bad_num"));

							map.put("id", itemJsonObject.getString("id"));
							map.put("uid", itemJsonObject.getString("uid"));
							map.put("comment_desc",
									itemJsonObject.getString("comment_desc"));
							map.put("created_at",
									itemJsonObject.getString("created_at")
											.substring(0, 10));
							map.put("name", itemJsonObject.getString("name"));
							map.put("avatar",
									itemJsonObject.getString("avatar"));
							map.put("avg_score",
									itemJsonObject.getString("total_score"));
							map.put("desc",
									"￥ " + itemJsonObject.getString("cash")
											+ "/"
											+ itemJsonObject.getString("unit"));

							taskList.add(map);
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return taskList;
	}

}
